//
//  HMPraiseTool.m
//  HowToDo
//
//  Created by 宋轩 on 17/1/8.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMPraiseTool.h"
#import "HMPraiseModel.h"

@implementation HMPraiseTool
+ (instancetype)sharedPraiseTool
{
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HMPraiseTool alloc] init];
    });
    return instance;
}

- (NSMutableArray *)modelArray
{
    if (_modelArray == nil)
    {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}


- (void)getPraiseDataWithID:(NSString *) readID_id andLike_count:(NSString *)like_count
{
    HMPraiseModel *praiseModel = [[HMPraiseModel alloc] init];
    [praiseModel setValue:readID_id forKey:@"readID_id"];
    
    NSString *count = [NSString stringWithFormat:@"%zd", like_count.integerValue + 1];
    
    [praiseModel setValue:count forKey:@"like_count"];

    [self.modelArray addObject:praiseModel];
    
    [self saveToCaches];
}

//- (void)getShoppingCarDataWithID:(NSString *) readID_id andItem_title:(NSString *)item_title andAdd_time:(NSString *)add_time andLike_count:(NSString *)like_count andItem_image_url:(NSString *)item_image_url andItem_introduce:(NSString *)item_introduce andRead_count:(UIButton *)read_count
//{
//    HMPraiseModel *praiseModel = [[HMPraiseModel alloc] init];
//    [praiseModel setValue:readID_id forKey:@"readID_id "];
//    [praiseModel setValue:item_title forKey:@"item_title"];
//    [praiseModel setValue:add_time forKey:@"add_time"];
//    [praiseModel setValue:like_count forKey:@"like_count"];
//    [praiseModel setValue:item_image_url forKey:@"item_image_url"];
//    [praiseModel setValue:read_count forKey:@"read_count"];
//    
//    [self.modelArray addObject:praiseModel];
//    
//    [self saveToCaches];
//}

- (void)removePraiseDataWithID:(NSString *) readID_id
{
    [self.modelArray enumerateObjectsUsingBlock:^(HMPraiseModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.readID isEqualToString:readID_id])
        {
            NSString *count = [NSString stringWithFormat:@"%zd", obj.like_count.integerValue + 1];
            
            [obj setValue:count forKey:@"like_count"];
        }
    }];
    
    [self saveToCaches];
}

- (void)saveToCaches
{
    NSString *cachesStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *fileName = [cachesStr stringByAppendingString:@"praiseCount.plist"];
    
    [NSKeyedArchiver archiveRootObject:self.modelArray toFile:fileName];
}

- (NSString *)getLikeCountWithID:(NSString *)reaadID_id
{
    NSString *cachesStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *fileName = [cachesStr stringByAppendingString:@"content.plist"];
    
    NSArray *countArr = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
    
    NSString *tid = nil;
    
    for (NSInteger i = 0; i < countArr.count; i++)
    {
        if ([[countArr[i] valueForKey:@"readID_id"] isEqualToString:reaadID_id])
        {
            tid = [countArr[i] valueForKey:@"like_count"];
        }
    }
    
    return tid;
}
@end
